package com.ef.web.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ef.constant.ETimeline;
import com.ef.domain.ClassEntity;
import com.ef.domain.LeadsClassEntity;
import com.ef.service.ClassArrangeService;
import com.ef.service.ClassBookingService;
import com.ef.service.LeadsClassEntityService;
import com.ef.service.LeadsService;
import com.ef.util.CalendarUtil;

/**
 * 排课. <br/>
 * 
 * @author CYG create:2009-12-13 上午09:06:34
 */
public class ClassCheckinAction extends BaseAction<ClassEntity> {

	private static final long serialVersionUID = 2008996134431247542L;

	private LeadsClassEntityService leadsClassEntityService;

	private ClassArrangeService classArrangeService;

	private ClassBookingService classBookingService;

	private LeadsService leadsService;

	private List<LeadsClassEntity> leadsClassEntityList;

	private Map<String, String> checkMessageMap = new LinkedHashMap<String, String>();

	public ClassCheckinAction() {
	
	}

	/**
	 * 初始化今天
	 * 
	 * @param model
	 */
	protected void prepareModelInner(ClassEntity model) {
		if (model != null && model.getClassDate() == null) {
			model.setClassDate(new Date());
		}
		List<ETimeline> times = null;
		if (CalendarUtil.isWeekend(model.getClassDate())) {
			times = ETimeline.getWeekendTimeline();
		} else {
			times = ETimeline.getNomarlTimeline();
		}
		checkMessageMap.clear();
		for (ETimeline timeline : times) {
			String msg = classArrangeService.getClassCheckMessage(model
					.getClassDate(), timeline);
			checkMessageMap.put(timeline.name(), timeline.toString() + "    "
					+ msg);
		}
	}

	public String selectCheckinEntity() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			ClassEntity entity = null;
			String entityid = request.getParameter("entityid");
			if (entityid != null && entityid.trim().length() > 0) {
				entity = classArrangeService.findById(Long.valueOf(entityid));
			}
			;
			if (entity == null || entity.getId() == null) {
				throw new Exception(" Can't get class ");
			}
			return responseSelectEntity(entity);
		} catch (Exception e) {
			return responseError(e.getMessage());
		}
	}

	/**
	 * Ajax 选择具体的课程
	 * 
	 * @return
	 */
	public String responseSelectEntity(ClassEntity entity) {
		try {
			StringBuffer rsp = new StringBuffer();
			rsp.append(entity.getResponseString());
			List<LeadsClassEntity> leadsClasses = leadsClassEntityService
					.getLeadsClassEntityListWithClassEntity(entity);
			for (LeadsClassEntity lentity : leadsClasses) {
				rsp.append(lentity.getResponseString());
			}
			// System.out.println(rsp.toString());
			response(rsp.toString());
		} catch (Exception e) {
			responseError(e.getMessage());
		}
		return null;
	}

	/**
	 * Ajax 选择哪些课和老师可选
	 * 
	 * @return
	 */
	public String selectCheckinTime() {
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String date = request.getParameter("curdate");
			String time = request.getParameter("curtime");
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			df.parse(date.trim());
			List<ClassEntity> classEntiys = classArrangeService
					.getClassEntityByDateTime(df.parse(date.trim()), ETimeline
							.valueOf(time.trim()));
			StringBuffer rsp = new StringBuffer();
			for (ClassEntity entity : classEntiys) {
				if (entity.getTeacher() != null
						&& entity.getClassroom() != null) {
					rsp.append("<entity>").append("{classid:").append(
							entity.getId()).append(",tid:").append(
							entity.getTeacher().getId()).append(",tname:\"")
							.append(entity.getTeacher().getName()).append(
									"\",cid:").append(
									entity.getClassroom().getId()).append(
									",cname:\"").append(
									entity.getClassroom().getName()).append(
									"\",ctimes:").append(
									entity.getCheckedTimes()).append(
									"}</entity>");
				}
			}
			// System.out.println(rsp.toString());
			response(rsp.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}

	/**
	 * Ajax 完成 Checkin 工作
	 * 
	 * @return
	 */
	public String doCheckin() {
		String responseStr = "";
		if (this.getSessionUser() == null) {
			responseStr = "<type>error</type>"
					+ "<info>plesas login in first!</info>";
			return response(responseStr);
		}
		;
		HttpServletRequest request = ServletActionContext.getRequest();
		String classentityid = request.getParameter("classentityid");
		String leadsclassids = request.getParameter("leadsclassids");
		ClassEntity classEntiy = classArrangeService.findById(Long
				.valueOf(classentityid));
		classBookingService.doCheckin(classEntiy, leadsclassids,
				getSessionUser());
		classEntiy.addCheckedTimes();
		classArrangeService.saveOrUpdate(classEntiy);
		return responseSelectEntity(classEntiy);
	}
 	
	public Boolean isManager() {
		if (getSessionUser().getRole().getName().equalsIgnoreCase("CR")) {
			return false;
		} else {
			return true;
		}
	}

	public String beforeCheckin() {
		if (entity != null && entity.getClassDate() == null) {
			entity.setClassDate(new Date());
		}
		List<ETimeline> times = null;
		if (CalendarUtil.isWeekend(entity.getClassDate())) {
			times = ETimeline.getWeekendTimeline();
		} else {
			times = ETimeline.getNomarlTimeline();
		}
		checkMessageMap.clear();
		for (ETimeline timeline : times) {
			String msg = classArrangeService.getClassCheckMessage(entity
					.getClassDate(), timeline);
			checkMessageMap.put(timeline.name(), timeline.toString() + "    "
					+ msg);
		}
		return "beforeCheckin";
	}

	public LeadsClassEntityService getLeadsClassEntityService() {
		return leadsClassEntityService;
	}

	public void setLeadsClassEntityService(
			LeadsClassEntityService leadsClassEntityService) {
		this.leadsClassEntityService = leadsClassEntityService;
	}

	public List<LeadsClassEntity> getLeadsClassEntityList() {
		return leadsClassEntityList;
	}

	public void setLeadsClassEntityList(
			List<LeadsClassEntity> leadsClassEntityList) {
		this.leadsClassEntityList = leadsClassEntityList;
	}

	public ClassArrangeService getClassArrangeService() {
		return classArrangeService;
	}

	public void setClassArrangeService(ClassArrangeService classArrangeService) {
		this.classArrangeService = classArrangeService;
	}

	public LeadsService getLeadsService() {
		return leadsService;
	}

	public void setLeadsService(LeadsService leadsService) {
		this.leadsService = leadsService;
	}

	public ClassBookingService getClassBookingService() {
		return classBookingService;
	}

	public void setClassBookingService(ClassBookingService classBookingService) {
		this.classBookingService = classBookingService;
	}

	public Map<String, String> getCheckMessageMap() {
		return checkMessageMap;
	}

	public void setCheckMessageMap(Map<String, String> checkMessageMap) {
		this.checkMessageMap = checkMessageMap;
	}

}
